<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<script src="/public/static/js/jquery-3.31.min.js"></script>
<script>
    var tm_url = getQueryString('tm_url')||window.sessionStorage.getItem('tm_url')||'';
    var componentUrl = getQueryString('componentUrl')||window.sessionStorage.getItem('tm_componentUrl')||'';
    var code = getQueryString('code')||'';
    $(document).ready(function() {
        var postData = {tm_url:tm_url};
        if (code){
            postData.code = code;
        };
        if (tm_url){
            window.sessionStorage.setItem('tm_url',tm_url)
        };
        if (componentUrl){
            postData.componentUrl = componentUrl;
            window.sessionStorage.setItem('tm_componentUrl',componentUrl);
        }
        $.ajax({
            url: '/api/Mocdistribution/tokenDistributionUser',
            type: 'post',
            data: postData,
            cache: false,  //禁用缓存
            dataType: 'json',
            success: function (data) {
                if(data.code>300 && data.code<310){
                    window.location.replace(data.data.moc_auth_url);
                }else if (data.code==200) {
                    window.location.replace(data.data.tm_url);
                    window.sessionStorage.removeItem('tm_componentUrl');
                }else{
                    window.sessionStorage.removeItem('tm_componentUrl');
                    var msg = data.data.moc_auth_url?(data.msg+',是否需要重新认证？'):data.msg;
                    var infoConfirm=confirm(msg);
                    if (infoConfirm==true)
                    {
                        if (data.data.moc_auth_url) {
                            window.location.replace(data.data.moc_auth_url);
                        }else{
                            closeWindow();
                        }
                    } else {
                        closeWindow();
                    }
                }
            },
            error:function(){

            }
        });

    });
    function closeWindow(){
        if (navigator.userAgent.indexOf('MSIE') > 0) { // close IE
            if (navigator.userAgent.indexOf('MSIE 6.0') > 0) {
                window.opener = null;
                window.close();
            } else {
                window.open('', '_top');
                window.top.close();
            }
        } else { // close chrome;It is effective when it is only one.
            window.opener = null;
            window.open('', '_self');
            window.close();
        }
    };
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }
</script>
</body>
</html>